// pages/wuxianchi/wuxianchi.js
const {
  getwuxiangame,
  getUserInfo,
  raffle,
  getraf
} = require("../../../utils/api")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navbar: {
      top: `${wx.getMenuButtonBoundingClientRect().top}`,
      height: `${wx.getMenuButtonBoundingClientRect().height}`
    },
    navnum: '0',//导航栏选择点
    zhongjiang: 'https://7072-prod-9gbx4romf8b968e1-1316521626.tcb.qcloud.la/images/wuxianchi/zhongjianglist.png',
    nav: [
      { name: '分享', id: 0 },
      { name: '收藏', id: 1 },
      { name: '说明', id: 2 },
      { name: '中奖记录', id: 3 },
    ],
    raffles: [
      { num: 1 },
      { num: 3 },
      { num: 5 },
      { num: 10 },
    ],//抽奖按钮
    show: false,//抽奖弹窗
    ba1: '',//背景图
    img: {},
    uuid: '',//无限赏uuid
    name: '',//无限赏name
    gameuser: {},//获取的无限赏内容
    user: {},//用户信息
    radio: 'stored_money',//单选内容
    total: 0,//获奖总概率
    raffle_count: '',//抽奖次数
    jiangshow: false,//中奖弹窗
    jianglist: [],//获得的奖励
    newjianglist: [],//获得的奖励信息
    isclick: true,//节流开关
    prizelist: [],//中奖记录列表
    newprizelist: [],//获取到的中奖赏品信息
    icon: {
      normal: 'https://7072-prod-9gbx4romf8b968e1-1316521626.tcb.qcloud.la/images/Raffles/avt.png',
      active: 'https://7072-prod-9gbx4romf8b968e1-1316521626.tcb.qcloud.la/images/Raffles/selavt.png',
    },
    checked: false,//复选框
    imgshow: false,//赏品图片详情
    imgdel: {},//图片详情信息
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      name: options.name,
      uuid: options.uuid
    })
  },
  //获取用户信息
  getuser() {
    getUserInfo().then(res => {
      console.log('用户信息', res)
      if (res.data.openid == '') {
        wx.showModal({
          title: '提示',
          content: '您还没有登录，点击确定跳转',
          success(res) {
            if (res.confirm) {
              console.log(res)
              wx.navigateTo({
                url: '/pages/login/login',
              })
            } else if (res.cancel) {
              wx.navigateBack({
                delta: 1,
              })
            }
          },
        })
      } else {
        wx.setStorageSync('User', res.data)
        this.setData({ user: res.data })
      }
    })
  },
  //获取无限赏内容
  getwuxian() {
    getwuxiangame({ name: this.data.name, uuid: this.data.uuid }).then(res => {
      console.log('无限赏数据', res)
      this.setData({
        gameuser: res.data
      })
      // this.foreach()
    })
  },
  ondarw(e) {
    this.setData({ show: true, raffle_count: e.currentTarget.dataset.num })
  },

  //关闭弹窗
  onClose() { this.setData({ show: false }); },
  onChange(event) {
    console.log(event.detail)
    this.setData({
      radio: event.detail,
    });
  },

  fuChange(event) {
    this.setData({
      checked: event.detail,
    });
  },
  //奖励弹窗
  onClosejiang() {
    this.setData({ jiangshow: false })
  },
  //循环数组中的内容
  foreach() {
    let gailv = this.data.gameuser.goods
    // console.log(gailv)
    let a = 0
    gailv.forEach(function (val, idx, arr) {
      a += val.wight
    }, 0)
    // console.log(a)
    a = a / 100
    this.setData({ total: a })
  },
  //抽奖
  clickraffle() {
    this.setData({ isclick: false })
    wx.showLoading({
      title: '抽奖中',
    })
    raffle({ game_uuid: this.data.uuid, pay_type: this.data.radio, raffle_count: this.data.raffle_count }).then(res => {
      console.log('抽奖获得的赏品', res)
      if (res.statusCode == 200) {
        wx.hideLoading()
        this.getuser()
        this.setData({ jianglist: res.data.goods, show: false, jiangshow: 'true' })
        setTimeout(e => {
          this.newjiang()
          this.setData({ isclick: true })
        }, 300)
      } else {
        wx.hideLoading()
        wx.showToast({
          title: '抽奖失败',
          icon: 'error'
        })
        setTimeout(e => {
          this.setData({ isclick: true })
        }, 300)
      }
    })
  },
  newjiang() {
    var arr1 = this.data.gameuser.goods
    var arr2 = this.data.jianglist;
    var newArr = [];
    for (var i = 0; i < arr1.length; i++) {
      for (var j = 0; j < arr2.length; j++) {
        if (arr1[i].uuid === arr2[j].uuid) {  // 找到相同的数据追加到新的数组中
          newArr.push(arr1[i]);
        }
      }
    }
    console.log(newArr);
    this.setData({ newjianglist: newArr })
  },
  //点击中奖记录
  jilu() {
    if (this.data.navnum == 0) {
      this.setData({
        zhongjiang: 'https://7072-prod-9gbx4romf8b968e1-1316521626.tcb.qcloud.la/images/wuxianchi/yulan.png',
        navnum: 1
      })
      this.getrecord()

    } else {
      this.setData({
        zhongjiang: 'https://7072-prod-9gbx4romf8b968e1-1316521626.tcb.qcloud.la/images/wuxianchi/zhongjianglist.png',
        navnum: 0
      })
    }
  },
  //获取抽奖记录
  getrecord() {
    getraf({ list: 1, page: 1, num: 20 }).then(res => {
      console.log('中奖记录', res)
      if (res.statusCode == 200) {
        this.setData({ prizelist: res.data })
        this.newlist()
      }
    }).catch(err => {
      console.log(err)
    })
  },
  //筛选赏品信息
  newlist() {
    var arr1 = this.data.gameuser.goods
    var arr2 = this.data.prizelist;
    var newArr = [];
    for (var i = 0; i < arr1.length; i++) {
      for (var j = 0; j < arr2.length; j++) {
        if (arr1[i].uuid === arr2[j].record.good_uuid) {  // 找到相同的数据追加到新的数组中
          let arr = arr1[i]
          arr['createdAt'] = arr2[j].record.createdAt
          arr['user'] = arr2[j].user
          newArr.push(arr1[i]);
        }
      }
    }
    console.log(newArr);
    this.setData({ newprizelist: newArr })
  },
  //去赏袋
  toshangdai() {
    wx.switchTab({
      url: '/pages/shangdai/shangdai',
    })
  },
  //赏品图片i详情
  onimgshow() {
    this.setData({ imgshow: false })
  },
  onimgdel(e) {
    console.log(e)
    let del = e.currentTarget.dataset
    this.setData({ imgshow: true, imgdel: del })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getuser()
    this.getwuxian()
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(res) {
    if (res.from === 'button') {
      console.log(res.target)
    }
    return {
      title: "分享",
      path: '/pages/quanjushang/quanjushang',
      success: function (res) {
        console.log('成功', res)
      }
    }
  }
})